package doc2db.interfaces.action;

import doc2db.DbManager.DbSelects;
import doc2db.Keey;
import doc2db.Launcher;
import doc2db.UserProp;
import doc2db.fileWorker.FileBeginer;
import doc2db.fileWorker.FileWorker;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;

/**
 *
 * @author yuriy
 */
public class CreateDialog {
    protected JEditorPane p;
    protected JEditorPane e;
    protected String toolTipeText [] ;
    protected DefaultListModel model = new DefaultListModel();
    protected JList list = new JList(model) {
        @Override
        public String getToolTipText(MouseEvent e) {
            int index = locationToIndex(e.getPoint());
            if (-1 < index) {
                //return (String) getModel().getElementAt(index);
                return toolTipeText[index];
            } else {
                return null;
            }
        }
    };
    private JScrollPane pane = new JScrollPane(list);
    public Object res = "__NULL";
    protected int resNum = 0;

    public CreateDialog(JEditorPane __p, JEditorPane t2){
        p = __p;
        e = t2;
        list.addListSelectionListener(null);
        list.setFont(UserProp.textFont);
        pane.setPreferredSize(new Dimension(40, 80));
    }

    public void setRes(String r){
        res = r;
    }

    protected void show(){
        Object []result = null;
        result = init(result);
        if(result == null)
            result = init(result);
        if(result == null){
            System.err.println("Ошибка ввода/ввывода при отправке бэкенду.");
            return;
        }
        toolTipeText =this.initToolTipeText(result);
        model.clear();
        for (int i = 0; i < result.length; i++) {
            model.addElement(result[i]);
        }
        JDialog d = new D(null);
        d.setVisible(true);
    }

    protected String [] initToolTipeText(Object s []){
        String [] retStr = new String [s.length];
        for (int i = 0; i < retStr.length; i++) {
            retStr[i] = String.valueOf(s[i]);
        }
        return retStr;
    }

    protected class D extends JDialog{
        D(JFrame f){
            super(f, "doc2db - Список таблиц", true);
            Icon cnclicon = Launcher.getIcon("pane/button_cancel.png");
            final Action ca = new AbstractAction("Отмена",cnclicon) {

                @Override
                public void actionPerformed(ActionEvent e) {
                    D.this.setVisible(false);
                }
            };

            Icon okicon = Launcher.getIcon("pane/button_ok.png");
            final Action a = new AbstractAction("Принять",okicon) {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Object selectedValue = list.getSelectedValue();
                    if (selectedValue != null) {
                        res = selectedValue;//.toString();
                        resNum = list.getSelectedIndex();
                        setVisible(false);
                    }
                }
            };
            list.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent mouseEvent) {
                    if (mouseEvent.getClickCount() == 2) {
                        a.actionPerformed(null);
                }
            }
            });
            Container c = getContentPane();
            c.add(new JLabel("Список таблиц"), BorderLayout.NORTH);
            c.add(pane, BorderLayout.CENTER);

            JButton b = new JButton(a);
            JButton b2 = new JButton(ca);

            KeyStroke press = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
            KeyStroke release1 = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true);

            InputMap map = b2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
            map.put(press,"pressed");
            map.put(release1,"released");

            JPanel sp = new JPanel();
            sp.add(b);
            sp.add(b2);
            c.add(sp, BorderLayout.SOUTH);

            setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
            pack();
            setSize(new Dimension(getSize().width, 400));
            setLocation(100, 100);
        }
    }

    protected Object[] init(Object[] result) {
        UserProp up = new UserProp();
        String str = FileWorker.howInit(FileBeginer.resurse_resurseFolder + Keey.sp
                + FileBeginer.sqlSelects, "tableList");
        try {
            DbSelects query = new DbSelects(up.getURL(), up.getUserProp());
            if (query.sendQuery(str)) {
                int rows = query.getRows();
                result = new String[rows];
                for (int i = 1; i <= rows; i++) {
                    result[i - 1] = "" + query.getString(i, "relname");
                }
            }
        } catch (SQLException ex) {
        } catch (ClassNotFoundException ex) {
        }
        return result;
    }

}
